package com.example.optimizaapp;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SeleccionarClienteActivity extends Activity {
	
	final String[] clientes = {"Cliente 1", "Cliente 2", "Cliente 3"};
	private ListView mainListView;
	private ArrayAdapter<String> listAdapter ;	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_seleccionar_cliente);
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	    // Find the ListView resource.  
	    this.mainListView=(ListView) findViewById(R.id.listViewClientes);
	    // Create and populate a List of clients.
	    ArrayList<String> listaClientes = new ArrayList<String>();
	    listaClientes.addAll(Arrays.asList(this.clientes));
	    // Create ArrayAdapter using the clients list.
	    listAdapter= new ArrayAdapter<String>(this, R.layout.simple_row, listaClientes);
	    this.mainListView.setAdapter(listAdapter);
	    
	    this.mainListView.setOnItemClickListener(new OnItemClickListener() {
	    	public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
	    	    String item = (String) adapter.getItemAtPosition(position);
	    	    Intent returnIntent = new Intent();
	    	    returnIntent.putExtra("result",item);
	    	        	    
	    	    if (getParent() == null) {
	    	    	
	    	        setResult(Activity.RESULT_OK, returnIntent);
	    	    } else {
	    	        getParent().setResult(Activity.RESULT_OK, returnIntent);
	    	    }
	    	    finish();
	    	}
	    });
	}
	
	

}
